package com.vanke.kotlin.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.vanke.kotlin.App
import org.jetbrains.anko.db.*

/**
 * @name Kotlin
 * @package name：com.vanke.kotlin.db
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/7/24 14:12
 */
class ForecastDbHelper(context: Context = App.instance) : ManagedSQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {

    companion object {
        val DB_NAME = "forecast.db"
        val DB_VERSION = 1
        val instance: ForecastDbHelper by lazy { ForecastDbHelper() }
    }

    override fun onCreate(db: SQLiteDatabase) {
//        db.createTable(CityForecastTable.NAME,true,
//                    Pair(CityForecastTable.ID, INTEGER+ PRIMARY_KEY),
//                    Pair(CityForecastTable.CITY, TEXT),
//                    Pair(CityForecastTable.COUNTRY, TEXT)
//                )
        db.createTable(CityForecastTable.NAME, true,
                CityForecastTable.ID to INTEGER + PRIMARY_KEY,
                CityForecastTable.CITY to TEXT,
                CityForecastTable.COUNTRY to TEXT
        )

        db.createTable(DayForecastTable.NAME, true,
                DayForecastTable.ID to SqlType.create("INTEGER PRIMARY KEY AUTOINCREMENT"),
                DayForecastTable.DATE to INTEGER,
                DayForecastTable.DESCRIPTION to TEXT,
                DayForecastTable.HIGH to INTEGER,
                DayForecastTable.LOW to INTEGER,
                DayForecastTable.ICON_URL to TEXT,
                DayForecastTable.CITY_ID to INTEGER
        )
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.dropTable(CityForecastTable.NAME,true)
        db.dropTable(DayForecastTable.NAME,true)
        onCreate(db)
    }


}